package com.sky.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 记录方法执行时间
 */
@Component
@Aspect
@Slf4j
public class RecordTimeAspect {
    /**
     * 定义切入点表达式
     * 匹配com.sky.controller包及其子包下的所有类的所有方法
     */
    @Pointcut("execution(* com.sky.controller..*(..))")
    private void recordTimePointCut() {}

    /**
     * 定义环绕通知类型，并使用切入点表达式
     * @param joinPoint 连接点
     * @return 执行返回对象
     */
    @Around("recordTimePointCut()")
    public Object recordTimeAround(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object proceed = joinPoint.proceed();
        long endTime = System.currentTimeMillis();
        log.info("执行方法：{}", joinPoint.getSignature());
        log.info("执行时间：{}ms", endTime - startTime);
        return proceed;
    }
}
